package com.dmap.admin.controller.background;

import com.dmap.admin.bo.user.UserInfo;
import com.dmap.admin.constants.AdminConstant;
import com.dmap.admin.controller.background.param.SelectUserParam;
import com.dmap.admin.controller.background.param.UserParam;
import com.dmap.admin.po.AdminUser;
import com.dmap.admin.service.backgroundv1.RoleService;
import com.dmap.admin.service.backgroundv1.UserService;
import com.dmap.admin.vo.UserVo;
import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * \* User: silent
 * \* Date: 08/12/17 Time: 10:50
 * \* Description:
 * \
 */
@Api(value = "用户管理", description = "background/用户管理")
@Controller
@RequestMapping(value = "/background/pmpd-admin-server/v1/user/")
public class UserManageController {

    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;

    @ResponseBody
    @ApiOperation(value = "新增用户", notes = "必填： account, password, roleId")
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public BaseVo insertUser(@RequestBody UserParam param) {
        BaseVo baseVo = new BaseVo();
        if (param == null || StringUtils.isEmpty(param.getAccount())
                || StringUtils.isEmpty(param.getPassword()) || StringUtils.isEmpty(param.getRoleId())) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        if (userService.isExistSelectByAccount(param.getAccount())) {
            baseVo.setCode(AdminConstant.RESPONSE_ACCOUNT_EXIST_ERROR_CODE);
            baseVo.setMsg(AdminConstant.RESPONSE_ACCOUNT_EXIST_ERROR_MSG);
            return baseVo;
        }
        boolean insert = userService.insertUser(param);
        if (!insert) {
            baseVo.setCode(Constant.RESPONSE_INSERT_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
            return baseVo;
        }
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "修改用户", notes = "必填： id, account, password, roleId")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public BaseVo updateUser(@RequestBody UserParam param) {
        BaseVo baseVo = new BaseVo();
        if (param == null || StringUtils.isEmpty(param.getId()) || StringUtils.isEmpty(param.getAccount())
                || StringUtils.isEmpty(param.getPassword()) || StringUtils.isEmpty(param.getRoleId())) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        AdminUser adminUser = userService.selectUserById(Long.valueOf(param.getId()));
        if (adminUser == null) {
            baseVo.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
            return baseVo;
        }
        if (adminUser.getAccount().equals(AdminConstant.SUPER_ADMIN_USER)) {
            baseVo.setCode(AdminConstant.RESPONSE_OPERATE_ERROR_CODE);
            baseVo.setMsg(AdminConstant.RESPONSE_OPERATE_ERROR_MSG);
            return baseVo;
        }
        boolean update = userService.updateUser(param);
        if (!update) {
            baseVo.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
            return baseVo;
        }
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "删除用户", notes = "删除用户")
    @RequestMapping(value = "delete/{userId}", method = RequestMethod.DELETE)
    public BaseVo deleteUser(@PathVariable(value = "userId") String userId) {
        BaseVo baseVo = new BaseVo();
        if (StringUtils.isEmpty(userId)) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        AdminUser adminUser = userService.selectUserById(Long.valueOf(userId));
        if (adminUser == null) {
            baseVo.setCode(Constant.RESPONSE_DELETE_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_DELETE_ERROR_MSG);
            return baseVo;
        }
        if (adminUser.getAccount().equals(AdminConstant.SUPER_ADMIN_USER)) {
            baseVo.setCode(AdminConstant.RESPONSE_OPERATE_ERROR_CODE);
            baseVo.setMsg(AdminConstant.RESPONSE_OPERATE_ERROR_MSG);
            return baseVo;
        }
        boolean delete = userService.deleteUser(Long.valueOf(userId));
        if (!delete) {
            baseVo.setCode(Constant.RESPONSE_DELETE_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_DELETE_ERROR_MSG);
            return baseVo;
        }
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "查找用户",
            notes = "1.该查询用户信息接口采用模糊查询，如：输入“a”,则会将所有账号中带\"a\"字母的账号都查询出来；\n" +
                    "2.如果account为空，则查询整个用户列表;" +
                    "3.pageIndex和pageSize为必填。")
    @RequestMapping(value = "select", method = RequestMethod.POST)
    public BaseVo selectUser(@RequestBody SelectUserParam param) {
        BaseVo baseVo = new BaseVo();
        if (param.getPageIndex() == null || param.getPageSize() == null) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        if (param.getPageSize() == 0) {//这种后台为查询分页赋默认值的做法，不合适，后期需要优化
            param.setPageSize(AdminConstant.DEFAULT_PAGE_SIZE);
        }
        UserVo userInfoList;
        if (StringUtils.isEmpty(param.getAccount())) {
            userInfoList = userService.selectUserInfo(null, param.getPageIndex(), param.getPageSize());
        } else {
            userInfoList = userService.selectUserInfo(param.getAccount(), param.getPageIndex(), param.getPageSize());
        }
        baseVo.setData(userInfoList);
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "根据id查询用户", notes = "必填： id")
    @RequestMapping(value = "select/selectUserById", method = RequestMethod.GET)
    public BaseVo selectUserById(@RequestParam String id) {
        BaseVo baseVo = new BaseVo();
        if (StringUtils.isBlank(id)) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        AdminUser adminUser = userService.selectUserById(Long.valueOf(id));
        if (adminUser == null) {
            return baseVo;
        }
        UserInfo userInfo = new UserInfo(adminUser);
        baseVo.setData(userInfo);
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "根据account查询用户", notes = "必填： account")
    @RequestMapping(value = "select/selectUserByAccount", method = RequestMethod.GET)
    public BaseVo selectUserByAccount(@RequestParam String account) {
        BaseVo baseVo = new BaseVo();
        if (StringUtils.isBlank(account)) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        UserInfo userInfo = userService.selectUserByAccount(account);
        baseVo.setData(userInfo);
        return baseVo;
    }
}
